package com.hengpeng.itfinoss.controller.stm;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.persist.SystemConfig;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.service.ServiceFactory;

@Controller
@RequestMapping("/scm")
public class SystemConfigController extends BaseController
{

	@Autowired
	private ServiceFactory serviceFactory;

	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String preCreate(ModelMap map)
	{
		map.put("systemConfig", new SystemConfig());
		return "stm/sysconfig/detail";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String add(String id, String description, String value, ModelMap map)
	{
		if (id == null || id.trim().length() == 0)
			return returnError(map, "系统参数名不能为空！");
		DynamicQuery query = new DynamicQuery(SystemConfig.class);
		query.eq("id", id);
		List<SystemConfig> list = serviceFactory.getPersistService().find(SystemConfig.class, query);
		if (list != null && list.size() > 0)
			return returnError(map, "你输入的系统参数名称已存在！");
		SystemConfig sysconfig = new SystemConfig();
		sysconfig.setId(id);
		sysconfig.setDescription(description);
		sysconfig.setValue(value);
		try
		{
			ServiceResult<SystemConfig> result = serviceFactory.getSystemConfigService().create(sysconfig);
			return returnSuccess(map, result, "/scm/list");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return returnError(map, e);
		}
	}

	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public String update(String oldId, String description, String value, ModelMap map)
	{
		if (oldId == null || oldId.trim().length() == 0)
			return returnError(map, "系统参数名不能为空！");
		SystemConfig sysconfig = serviceFactory.getPersistService().get(SystemConfig.class, oldId);
		sysconfig.setDescription(description);
		sysconfig.setValue(value);
		try
		{
			ServiceResult<SystemConfig> result = serviceFactory.getSystemConfigService().modfiy(sysconfig);
			return returnSuccess(map, result, "/scm/list");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return returnError(map, e);
		}
	}

	@RequestMapping(value = "/remove")
	public String delete(ModelMap map, HttpServletRequest request, String[] ids)
	{
		if (ids == null || ids.length <= 0)
		{
			return returnError(map, "要删除的系统参数不能为空");
		}
		try
		{
			Set<Object> set = new HashSet<Object>(Arrays.asList(ids));
			ServiceResult<SystemConfig> result = serviceFactory.getSystemConfigService().remove(set);
			return returnSuccess(request, map, result, "/scm/list");
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			return returnError(map, ex);
		}
	}

	@RequestMapping(value = "/list")
	public String list(HttpServletRequest request, ModelMap map, String id_test, String description, String value) {
		/** 保存表单 */
		map.put("id_test", id_test);
		map.put("description", description);
		map.put("value", value);
		Pagination page = new Pagination(request);
		page.setPageSize(this.getPageSize(request));
		try {
			LinkedHashMap<String, OrderType> sortMap = new LinkedHashMap<String, OrderType>();
			sortMap.put("id", OrderType.DESC);
			List<SystemConfig> list = serviceFactory.getSystemConfigService()
					.findSysConfigPage(id_test, description, value, page.currentPage, page.pageSize, sortMap)
					.getReturnValue();
			Long count = serviceFactory.getSystemConfigService().findSysConfigCount(id_test, description, value)
					.getReturnValue();
			page.setEntityCount(Integer.parseInt(count.toString()));
			map.put("sysconfiglist", list);
			map.put("sysconfigListPage", page);
			return "stm/sysconfig/sysconfig_list";
		} catch (Exception ex) {
			ex.printStackTrace();
			return returnError(map, ex);
		}
	}

	@RequestMapping(value = "/get")
	public String get(String id, ModelMap map) {
		if (id == null) {
			return returnError(map, "系统参数名称不能为空！");
		}
		try {
			if (id.startsWith(",")) {
				id = id.split(",")[1];
			}
			SystemConfig sysConfig = serviceFactory.getSystemConfigService().findById(id).getReturnValue();
			map.put("systemConfig", sysConfig);
			return "stm/sysconfig/detail";
		} catch (Exception ex) {
			ex.printStackTrace();
			return returnError(map, ex);
		}
	}
}
